﻿using System;
using Birkelund.QueryStrings.Tokens;
using NUnit.Framework;

namespace Birkelund.QueryStrings.Test
{
    public class CustomDateTimeTokenTest
    {
        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void ConstructorInitialize_Null()
        {
            CustomDateTimeToken token = new CustomDateTimeToken("a", null);
        }

        [Test]
        public void DefaultValue()
        {
            CustomDateTimeToken token = new CustomDateTimeToken("a");
            DateTime expected = default(DateTime);
            DateTime actual = token.DefaultValue;

            Assert.AreEqual(expected, actual);
        }

        [Test]
        public void DefaultValue_DateTimeFormat()
        {
            CustomDateTimeToken token = new CustomDateTimeToken("a");
            string expected = CustomDateTimeToken.DEFAULT_DATETIME_RENDER_FORMAT;
            string actual = token.DateTimeRenderFormat;

            Assert.AreEqual(expected, actual);
        }

        #region RenderTokenValue
        [Test]
        public void RenderTokenValue_CustomFormat()
        {
            CustomDateTimeToken token = new CustomDateTimeToken("a", "yyyy");
            
            string expected = 2010.ToString();
            string actual = token.RenderTokenValue(new DateTime(2010, 12, 31, 23, 59, 59));

            Assert.AreEqual(expected, actual);
        }

        [Test]
        public void RenderTokenValue_DefaultFormat()
        {
            CustomDateTimeToken token = new CustomDateTimeToken("a");

            string expected = "2010-12-31";
            string actual = token.RenderTokenValue(new DateTime(2010, 12, 31, 23, 59, 59));

            Assert.AreEqual(expected, actual);
        }

        [Test]
        public void RenderTokenValue_InvalidFormat()
        {
            CustomDateTimeToken token = new CustomDateTimeToken("a", "abc");
            
            string expected = "abc";
            string actual = token.RenderTokenValue(new DateTime(2010, 12, 31, 23, 59, 59));

            Assert.AreEqual(expected, actual);
        }

        [Test]
        public void RenderTokenValue_ReadValue_Format1()
        {
            Uri initialUrl = new Uri("http://www.test.dk/default.aspx?a=2010-12-31");

            CustomDateTimeToken token = new CustomDateTimeToken("a");
            DateTime expected = new DateTime(2010, 12, 31);
            DateTime actual = token.ReadValue(initialUrl);

            Assert.AreEqual(expected, actual);
        }

        [Test]
        public void RenderTokenValue_ReadValue_Format2()
        {
            Uri initialUrl = new Uri("http://www.test.dk/default.aspx?a=31-12-2010");

            CustomDateTimeToken token = new CustomDateTimeToken("a");
            DateTime expected = new DateTime(2010, 12, 31);
            DateTime actual = token.ReadValue(initialUrl);

            Assert.AreEqual(expected, actual);
        }

        [Test]
        public void RenderTokenValue_ReadValue_Format3()
        {
            Uri initialUrl = new Uri("http://www.test.dk/default.aspx?a=31-12-10");

            CustomDateTimeToken token = new CustomDateTimeToken("a");
            DateTime expected = new DateTime(2010, 12, 31);
            DateTime actual = token.ReadValue(initialUrl);

            Assert.AreEqual(expected, actual);
        }
        #endregion
    }
}
